package com.ruoyi.controller.middlesign.view;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.controller.middlesign.MiddlesMethod;
import com.ruoyi.service.middlesign.MiddlesignService;
import com.ruoyi.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 手动推送B2801
 * @date 2021/3/20 15:44
 */
@Controller
@RequestMapping("/middlesignYi")
public class MiddlesignYiController extends BaseController {

    @Autowired
    MiddlesignService middlesignService;

    private String prefix = "middlesign";

    /**
     * 跳转分页页面
     */
    @GetMapping()
    public String middlesignyi() {
        return prefix + "/middlesignyi";
    }

    @PostMapping("/getAuthorizationList")
    @ResponseBody
    public TableDataInfo getAuthorizationList() {
        List<Map<String, Object>> maps = new ArrayList<>();
        List<Map<String, Object>> hxqyList = middlesignService.findHxqyList();
        for (Map map : hxqyList) {
            String code = map.get("code").toString();
            String name = map.get("name").toString();
            List<Map<String, Object>> maps1 = MiddlesMethod.getAuthorizationList(code, name);
            maps.addAll(maps1);
        }
        return getDataTable(maps);
    }

    @PostMapping("/getHistoryData")
    @ResponseBody
    public TableDataInfo getHistoryData(@RequestParam Map map) {
        middlesignService.insertHistoryData(map);
        List<Map<String, Object>> maps1 = middlesignService.findHistoryData(map);
        return getDataTable(maps1);
    }


    @RequestMapping("pushHistoryData")
    @ResponseBody
    public BaseResult pushHistoryData(String data, String CREDITCODE, String CREDITNO, String CREDITNAME, String hxqyName, String hxqyCode, String table) {
        return middlesignService.pushHistoryData(data, CREDITCODE, CREDITNO, CREDITNAME, hxqyName, hxqyCode, table);
    }

    /**
     * 修改跳转
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmp) {
        mmp.put("historyData", middlesignService.findHistoryDataById(id));
        return prefix + "/historyedit";
    }

    /**
     * 保存修改
     * @param map
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(@RequestParam Map<String, Object> map) {
        return toAjax(middlesignService.updateHistoryDate(map));
    }
}
